﻿using System;
using System.Drawing;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace DeskNote
{
    public sealed class Customize : IDisposable
    {
        private string _message = string.Empty;
        private int _xcoord = 100;
        private int _ycoord = 100;
        private SolidBrush _color = new SolidBrush(Color.Black);
        private float _fontSize = 12;
        private string _format = "";
        Regex formatPattern = new Regex(@"\[([0-9]*),([0-9]*),(\w*),([0-9]*)\]", RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.ECMAScript);
        internal int X_Coord
        {
            get
            {
                return _xcoord;
            }
        }
        internal int Y_Coord
        {
            get
            {
                return _ycoord;
            }
        }
        internal Brush FontColor
        {
            get
            {
                return _color;
            }
        }
        internal float FontSize
        {
            get
            {
                return _fontSize;
            }
        }
        internal string Message
        {
            get
            {
                return _message;
            }
        }
        internal Customize(string message)
        {
            this._message = message;
            ParseMessage();
        }
        private void ParseMessage()
        {
            string colorName = "";
            if (formatPattern.IsMatch(_message))
            {
                if (formatPattern.Matches(_message).Count == 1)
                {
                    Match mt = formatPattern.Match(_message);
                    GroupCollection gc = mt.Groups;
                    int.TryParse(gc[1].Value, out _xcoord);
                    int.TryParse(gc[2].Value, out _ycoord);
                    Color myColor = Color.FromName(gc[3].Value.ToString());
                    float.TryParse(gc[4].Value.ToString(), out _fontSize);
                    _format = gc[0].Value.ToString();
                    if (myColor.IsKnownColor || IsColor(gc[3].Value.ToString(), out myColor))
                        _color = new SolidBrush(myColor);
                    else
                    {
                        using (ColorDialog cd = new ColorDialog())
                        {
                            cd.AllowFullOpen = true;
                            cd.AnyColor = true;
                            cd.FullOpen = true;
                            if (cd.ShowDialog() == DialogResult.OK)
                            {
                                _color = new SolidBrush(cd.Color);
                                colorName = cd.Color.Name;
                            }
                            else
                            {
                                _color = new SolidBrush(Color.Red);
                                colorName = "Red";
                            } UpdateColor(gc[3].Value.ToString(), colorName);
                        }
                    };
                }
                else
                {
                    _xcoord = Settings.X_Axis;
                    _ycoord = Settings.Y_Axis;
                    _color = new SolidBrush(Color.Red);
                    _fontSize = Settings.FontSize;
                }
            }
            else
            {
                _xcoord = Settings.X_Axis;
                _ycoord = Settings.Y_Axis;
                _color = new SolidBrush(Color.Red);
                _fontSize = Settings.FontSize;
            } ClearComments();
        }
        private void UpdateColor(string oldColor, string newColor)
        {
            newColor = string.IsNullOrEmpty(oldColor) ? "," + newColor + "," : newColor;
            oldColor = string.IsNullOrEmpty(oldColor) ? ",," : oldColor;
            _message = formatPattern.Replace(_message, _format.Replace(oldColor, newColor));
            FileIO.Message = _message;
        }
        private static bool IsColor(string name, out Color retColor)
        {
            retColor = Color.Red;
            Regex hexPattern = new Regex(@"([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])([a-f0-9][a-f0-9])", RegexOptions.Multiline | RegexOptions.IgnoreCase | RegexOptions.ECMAScript);
            if (hexPattern.IsMatch(name))
            {
                Match mt = hexPattern.Match(name);
                GroupCollection gc = mt.Groups;
                retColor = Color.FromArgb(int.Parse(gc[1].Value.ToString(), System.Globalization.NumberStyles.HexNumber),
                    int.Parse(gc[2].Value.ToString(), System.Globalization.NumberStyles.HexNumber),
                    int.Parse(gc[3].Value.ToString(), System.Globalization.NumberStyles.HexNumber),
                    int.Parse(gc[4].Value.ToString(), System.Globalization.NumberStyles.HexNumber));
                return true;
            }
            else
            {
                return false;
            }
        }
        private void ClearComments()
        {
            string cleanedMessage = _message.Replace(Environment.NewLine, "☻");//258
            string[] allValues = cleanedMessage.Split('☻');
            StringBuilder sb = new StringBuilder();
            foreach (string line in allValues)
            {
                if (!line.Trim().StartsWith("#", StringComparison.OrdinalIgnoreCase) && !(line.Trim() == _format))
                {
                    sb.AppendLine(line);
                }
            } _message = sb.ToString();
        }

        #region IDisposable Members

        public void Dispose()
        {
            _color.Dispose();
        }

        #endregion
    }
}
